﻿using System;
using System.Collections.Generic;
using UILib;
using UnityEngine;
using XMainClient.UI.UICommon;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient.UI
{
	// Token: 0x020018FE RID: 6398
	internal class DemoUI : DlgBase<DemoUI, DemoUIBehaviour>
	{
		// Token: 0x17003AA9 RID: 15017
		// (get) Token: 0x06010B17 RID: 68375 RVA: 0x004291F0 File Offset: 0x004273F0
		public override string fileName
		{
			get
			{
				return "DebugDlg";
			}
		}

		// Token: 0x17003AAA RID: 15018
		// (get) Token: 0x06010B18 RID: 68376 RVA: 0x00429208 File Offset: 0x00427408
		public override int layer
		{
			get
			{
				return 1;
			}
		}

		// Token: 0x17003AAB RID: 15019
		// (get) Token: 0x06010B19 RID: 68377 RVA: 0x0042921C File Offset: 0x0042741C
		public override bool autoload
		{
			get
			{
				return true;
			}
		}

		// Token: 0x17003AAC RID: 15020
		// (get) Token: 0x06010B1A RID: 68378 RVA: 0x00429230 File Offset: 0x00427430
		public override bool isMainUI
		{
			get
			{
				return true;
			}
		}

		// Token: 0x06010B1B RID: 68379 RVA: 0x00429243 File Offset: 0x00427443
		protected override void Init()
		{
			this._platform = XSingleton<XUpdater.XUpdater>.singleton.XPlatform;
		}

		// Token: 0x06010B1C RID: 68380 RVA: 0x000FEEFC File Offset: 0x000FD0FC
		public override void Reset()
		{
		}

		// Token: 0x06010B1D RID: 68381 RVA: 0x00429258 File Offset: 0x00427458
		public override void RegisterEvent()
		{
			base.uiBehaviour.m_Button.RegisterClickEventHandler(new ButtonClickEventHandler(this.OnButtonClick));
			base.uiBehaviour.m_PreviousButton.RegisterClickEventHandler(new ButtonClickEventHandler(this._onPreviousBtnClick));
			base.uiBehaviour.m_NextButton.RegisterClickEventHandler(new ButtonClickEventHandler(this._onNextBtnClick));
			base.uiBehaviour.m_Close.RegisterClickEventHandler(new ButtonClickEventHandler(this._onCloseBtnClick));
			base.uiBehaviour.m_Input.RegisterKeyTriggeredEventHandler(new InputKeyTriggeredEventHandler(this._onKeyTriggered));
			base.uiBehaviour.m_Input.RegisterSubmitEventHandler(new InputSubmitEventHandler(this.OnSubmit));
			base.uiBehaviour.m_Open.RegisterClickEventHandler(new ButtonClickEventHandler(this.OnFpsClick));
		}

		// Token: 0x06010B1E RID: 68382 RVA: 0x00429331 File Offset: 0x00427531
		protected override void OnShow()
		{
			this._refreshMessages();
			base.uiBehaviour.m_ScrollView.ResetPosition();
			base.uiBehaviour.m_Input.selected(true);
			base.uiBehaviour.m_Bg.SetActive(true);
		}

		// Token: 0x06010B1F RID: 68383 RVA: 0x00429370 File Offset: 0x00427570
		public override void OnUpdate()
		{
			this.UpdateFPS();
		}

		// Token: 0x06010B20 RID: 68384 RVA: 0x0042937C File Offset: 0x0042757C
		private bool _onPreviousBtnClick(IXUIButton go)
		{
			this._onKeyTriggered(base.uiBehaviour.m_Input, (KeyCode)273);
			return true;
		}

		// Token: 0x06010B21 RID: 68385 RVA: 0x004293A8 File Offset: 0x004275A8
		private bool _onNextBtnClick(IXUIButton go)
		{
			this._onKeyTriggered(base.uiBehaviour.m_Input, (KeyCode)274);
			return true;
		}

		// Token: 0x06010B22 RID: 68386 RVA: 0x004293D4 File Offset: 0x004275D4
		private bool _onCloseBtnClick(IXUIButton go)
		{
			base.uiBehaviour.m_Bg.SetActive(false);
			return true;
		}

		// Token: 0x06010B23 RID: 68387 RVA: 0x004293FC File Offset: 0x004275FC
		private void _onKeyTriggered(IXUIInput input, KeyCode key)
		{
			bool flag = key == (KeyCode)273;
			if (flag)
			{
				bool flag2 = this.inputs.Count == 0;
				if (flag2)
				{
					return;
				}
				bool flag3 = this.lastInput != null;
				if (flag3)
				{
					bool flag4 = this.lastInput.Previous != null;
					if (flag4)
					{
						this.lastInput = this.lastInput.Previous;
					}
				}
				else
				{
					this.lastInput = this.inputs.Last;
				}
			}
			else
			{
				bool flag5 = key == (KeyCode)274;
				if (!flag5)
				{
					return;
				}
				bool flag6 = this.inputs.Count == 0;
				if (flag6)
				{
					return;
				}
				bool flag7 = this.lastInput != null;
				if (flag7)
				{
					bool flag8 = this.lastInput.Next != null;
					if (flag8)
					{
						this.lastInput = this.lastInput.Next;
					}
				}
				else
				{
					this.lastInput = this.inputs.First;
				}
			}
			bool flag9 = this.lastInput != null;
			if (flag9)
			{
				base.uiBehaviour.m_Input.SetText(this.lastInput.Value);
			}
		}

		// Token: 0x06010B24 RID: 68388 RVA: 0x0042951A File Offset: 0x0042771A
		private void UpdateLastLabelPos(IXUILabel label)
		{
			this.lastMessageY -= (float)label.spriteHeight;
		}

		// Token: 0x06010B25 RID: 68389 RVA: 0x00429534 File Offset: 0x00427734
		private float GetLastLabelPos(IXUILabel label)
		{
			return this.lastMessageY - (float)(label.spriteHeight / 2);
		}

		// Token: 0x06010B26 RID: 68390 RVA: 0x00429558 File Offset: 0x00427758
		private void _refreshMessages()
		{
			Vector3 tplPos = base.uiBehaviour.m_MessagePool.TplPos;
			this.lastMessageY = tplPos.y;
			base.uiBehaviour.m_MessagePool.ReturnAll(false);
			base.uiBehaviour.m_ActiveMessages.Clear();
			foreach (string text in this.messages)
			{
				GameObject gameObject = base.uiBehaviour.m_MessagePool.FetchGameObject(false);
				IXUILabel ixuilabel = gameObject.GetComponent("XUILabel") as IXUILabel;
				ixuilabel.SetText(text);
				base.uiBehaviour.m_ActiveMessages.Add(ixuilabel);
				gameObject.transform.localPosition = new Vector3(tplPos.x, this.GetLastLabelPos(ixuilabel), 0f);
				this.UpdateLastLabelPos(ixuilabel);
			}
		}

		// Token: 0x06010B27 RID: 68391 RVA: 0x00429658 File Offset: 0x00427858
		public void AddMessage(string message)
		{
			bool flag = !base.IsVisible();
			if (!flag)
			{
				object obj = this.locker;
				lock (obj)
				{
					bool flag2 = (long)this.messages.Count > (long)((ulong)DemoUI.MAX_MESSAGE_COUNT);
					if (flag2)
					{
						this.messages.Dequeue();
					}
					this.messages.Enqueue(message);
					bool flag3 = this.messages.Count != base.uiBehaviour.m_ActiveMessages.Count;
					if (flag3)
					{
						bool flag4 = this.messages.Count - base.uiBehaviour.m_ActiveMessages.Count == 1;
						if (flag4)
						{
							GameObject gameObject = base.uiBehaviour.m_MessagePool.FetchGameObject(false);
							IXUILabel ixuilabel = gameObject.GetComponent("XUILabel") as IXUILabel;
							ixuilabel.SetText(message);
							base.uiBehaviour.m_ActiveMessages.Add(ixuilabel);
							Vector3 tplPos = base.uiBehaviour.m_MessagePool.TplPos;
							gameObject.transform.localPosition = new Vector3(tplPos.x, this.GetLastLabelPos(ixuilabel), 0f);
							this.UpdateLastLabelPos(ixuilabel);
						}
						else
						{
							this._refreshMessages();
						}
					}
					else
					{
						int num = -1;
						Vector3 tplPos2 = base.uiBehaviour.m_MessagePool.TplPos;
						this.lastMessageY = tplPos2.y;
						foreach (string text in this.messages)
						{
							IXUILabel ixuilabel2 = base.uiBehaviour.m_ActiveMessages[++num];
							ixuilabel2.SetText(text);
							ixuilabel2.gameObject.transform.localPosition = new Vector3(tplPos2.x, this.GetLastLabelPos(ixuilabel2), 0f);
							this.UpdateLastLabelPos(ixuilabel2);
						}
					}
					base.uiBehaviour.m_ScrollView.ResetPosition();
				}
			}
		}

		// Token: 0x06010B28 RID: 68392 RVA: 0x004298A0 File Offset: 0x00427AA0
		private void _addInput(string s)
		{
			bool flag = (long)this.inputs.Count > (long)((ulong)DemoUI.MAX_MESSAGE_COUNT);
			if (flag)
			{
				this.inputs.RemoveFirst();
			}
			this.inputs.AddLast(s);
			this.lastInput = null;
		}

		// Token: 0x06010B29 RID: 68393 RVA: 0x004298E6 File Offset: 0x00427AE6
		private void OnSubmit(IXUIInput go)
		{
			this.Submit();
		}

		// Token: 0x06010B2A RID: 68394 RVA: 0x004298F0 File Offset: 0x00427AF0
		private bool OnButtonClick(IXUIButton go)
		{
			this.Submit();
			return true;
		}

		// Token: 0x06010B2B RID: 68395 RVA: 0x0042990C File Offset: 0x00427B0C
		private void Submit()
		{
			string text = base.uiBehaviour.m_Input.GetText();
			this._addInput(text);
			string[] separator = new string[]
			{
				"\n"
			};
			string[] array = text.Split(separator, StringSplitOptions.None);
			for (int i = 0; i < array.Length; i++)
			{
				this.AddMessage("> " + array[i]);
				bool flag = !XSingleton<XCommand>.singleton.ProcessCommand(array[i]);
				if (flag)
				{
					this.AddMessage(string.Format("[ff0000]Invalid Command: {0}[-]", array[i]));
				}
			}
			base.uiBehaviour.m_Input.SetText("");
			base.uiBehaviour.m_Input.selected(true);
		}

		// Token: 0x06010B2C RID: 68396 RVA: 0x004299C8 File Offset: 0x00427BC8
		private bool OnFpsClick(IXUIButton go)
		{
			bool activeInHierarchy = base.uiBehaviour.m_Bg.activeInHierarchy;
			if (activeInHierarchy)
			{
				base.uiBehaviour.m_Bg.SetActive(false);
			}
			else
			{
				base.uiBehaviour.m_Bg.SetActive(true);
			}
			return true;
		}

		// Token: 0x06010B2D RID: 68397 RVA: 0x00429A1C File Offset: 0x00427C1C
		public void UpdateFPS()
		{
			//bool flag = !XSingleton<XTimerMgr>.singleton.NeedFixedUpdate;
			if (XSingleton<XTimerMgr>.singleton.NeedFixedUpdate)
			{
				//bool flag2 = !this._platform.IsPublish();
				if (!this._platform.IsPublish())
				{
					//bool showBuildLog = XSingleton<XGame>.singleton.ShowBuildLog;
					if (XSingleton<XGame>.singleton.ShowBuildLog)
					{
						string syncModeString = XSingleton<XGame>.singleton.GetSyncModeString();
						string text = "Debug Q";
						base.uiBehaviour.m_fps.SetText(string.Concat(new object[]
						{
							text,
							" Build:",
							XLinkTimeStamp.BuildDateTime.ToString(),
							"\nFps: ",
							XSingleton<XGame>.singleton.Fps.ToString("F1"),
							" Avg Fps: ",
							XSingleton<XGame>.singleton.FpsAvg.ToString("F1"),
							"\n",
							syncModeString,
							XSingleton<XClientNetwork>.singleton.ServerIP,
							"\nSend:",
							XSingleton<XClientNetwork>.singleton.SendBytes,
							" Recv:",
							XSingleton<XClientNetwork>.singleton.RecvBytes,
							" delay:",
							XSingleton<XServerTimeMgr>.singleton.GetDelay()
						}));
					}
					else
					{
						base.uiBehaviour.m_fps.SetText("");
					}
				}
			}
		}

		// Token: 0x06010B2E RID: 68398 RVA: 0x00429B84 File Offset: 0x00427D84
		public void Toggle()
		{
			bool flag = !base.IsVisible();
			if (flag)
			{
				this.SetVisible(true, true);
			}
			else
			{
				bool activeInHierarchy = base.uiBehaviour.m_Bg.activeInHierarchy;
				if (activeInHierarchy)
				{
					base.uiBehaviour.m_Bg.SetActive(false);
					base.uiBehaviour.m_Input.selected(false);
				}
				else
				{
					base.uiBehaviour.m_Bg.SetActive(true);
					base.uiBehaviour.m_Input.selected(true);
				}
			}
		}

		// Token: 0x06010B2F RID: 68399 RVA: 0x00429C10 File Offset: 0x00427E10
		public bool IsMainUIVisible()
		{
			return base.IsVisible() && base.uiBehaviour.m_Bg.activeInHierarchy;
		}

		// Token: 0x04007A1B RID: 31259
		public static uint MAX_MESSAGE_COUNT = 20U;

		// Token: 0x04007A1C RID: 31260
		private Queue<string> messages = new Queue<string>();

		// Token: 0x04007A1D RID: 31261
		private LinkedList<string> inputs = new LinkedList<string>();

		// Token: 0x04007A1E RID: 31262
		private LinkedListNode<string> lastInput = null;

		// Token: 0x04007A1F RID: 31263
		private float lastMessageY = 0f;

		// Token: 0x04007A20 RID: 31264
		private object locker = new object();

		// Token: 0x04007A21 RID: 31265
		private IPlatform _platform = null;
	}
}
